package com.zfcoding.redismybatisplus.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.zfcoding.redismybatisplus.entities.userentity.User;
import com.zfcoding.redismybatisplus.services.UserService;
import lombok.var;
import org.apache.ibatis.annotations.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    protected static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    //TODO ...异常中间件...
    /*
    用户.新增
     */
    @PostMapping
    public ResponseEntity postUser(User user){
        logger.info(" --- 用户.新增.开始 ---");

        var result = userService.insert(user);
        if (result  > 0){
            return new ResponseEntity(HttpStatus.OK);
        }
        logger.info(" --- 用户.新增.结束 ---");
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }


    /*
    用户.获取
     */
    @GetMapping
    public User getUser(int id){
        return userService.selectModel(id);
    }

    /*
    用户.删除
     */
    @DeleteMapping
    public ResponseEntity postDelUser(int id){
        var result = userService.deleteById(id);
        if (result > 0)
            return new ResponseEntity(HttpStatus.OK);
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }

    /*
    用户.更新
     */
    @PutMapping
    public ResponseEntity putUpdateUser(User user){
        var result = userService.updateById(user);
        if (result > 0)
            return new ResponseEntity(HttpStatus.OK);
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }

    /*
    测试.回调信息
     */
    @PostMapping("/callback")
    public Object testCallBack(@RequestBody Object obj){
        logger.info(" --- 回调.开始 ---");
        var result = userService.insertObject(obj);
        return result;
    }

}
